// cart.js  只干三件事：读 / 写 / 删
const CART_KEY = 'myCart';

// 读
export function getCart() {
  try {
    return JSON.parse(localStorage.getItem(CART_KEY)) || [];
  } catch {
    return [];
  }
}

// 写（追加）
export function addCartItem(item) {
  const list = getCart();
  list.push(item);
  localStorage.setItem(CART_KEY, JSON.stringify(list));
}

// 删
export function removeCartItem(id) {
  const list = getCart().filter(v => v.id !== id);
  localStorage.setItem(CART_KEY, JSON.stringify(list));
  return list;
}
